package com.yager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yager.mapper.CommentMapper;
import com.yager.model.Comment;
import com.yager.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void addNewComment(String userid, String typeid, String description) {
        String max=commentMapper.findMaxId();
        max=max.substring(1);

        Integer i=Integer.parseInt(max);
        i++;
        String id="C";

        if(i<10) id=id+"00"+i.toString();
        else if(i<100) id=id+"0"+i.toString();
        else id=id+i.toString();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String datetime=sdf.format(new Date());
        Date date= null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Comment comment=new Comment(id,date,userid,typeid,description);

        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(String commentid) {
        commentMapper.deleteByPrimaryKey(commentid);
    }

    @Override
    public PageInfo<Comment> getAllCommentByHouseId(String houseid,Integer pageNo) {
        PageHelper.startPage(pageNo,10);
        List<Comment> list=commentMapper.getCommentsByHouse(houseid);
        PageInfo<Comment> commentPageInfo=new PageInfo<>(list,5);

        return commentPageInfo;
    }

    @Override
    public PageInfo<Comment> getAllCommentByOrderId(String orderid,Integer pageNo) {
        PageHelper.startPage(pageNo,3);
        List<Comment> list=commentMapper.getCommentsByOrder(orderid);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(list,5);

        return commentPageInfo;
    }

    @Override
    public boolean isHouseHasComment(String houseid) {
        int count=commentMapper.getHouseCommentCount(houseid);
        if(count>0) return true;
        else return false;
    }

    @Override
    public boolean isOrderHasComment(String orderid) {
        int count=commentMapper.getOrderCommentCount(orderid);
        if(count>0) return true;
        else return false;
    }
}
